package com.luoshizhen.lease.web.admin.controller.apartment;


import com.luoshizhen.lease.common.result.Result;
import com.luoshizhen.lease.web.admin.service.FileService;
import io.minio.errors.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@Tag(name = "文件管理")
@RequestMapping("/admin/file")
@RestController
public class FileUploadController {

    @Autowired
    private FileService service;

    /**
     * 上传文件到服务器
     *
     * @param file 要上传的文件，类型为MultipartFile，适用于通过HTTP请求上传的文件
     * @return 返回一个Result对象，其中包含上传文件的URL
     * @throws ServerException 当服务器发生错误时抛出
     * @throws InsufficientDataException 当数据不足时抛出
     * @throws ErrorResponseException 当收到错误响应时抛出
     * @throws IOException 当发生I/O错误时抛出
     * @throws NoSuchAlgorithmException 当无法找到所需的哈希算法时抛出
     * @throws InvalidKeyException 当密钥无效时抛出
     * @throws InvalidResponseException 当响应无效时抛出
     * @throws XmlParserException 当XML解析失败时抛出
     * @throws InternalException 当发生内部错误时抛出
     */
    @Operation(summary = "上传文件")
    @PostMapping("upload")
    public Result<String> upload(@RequestParam MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 调用service层的upload方法上传文件，并获取文件的访问URL
        String url = service.upload(file);
        // 返回上传成功的结果，包含文件的URL
        return Result.ok(url);
    }
}
